﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Kpmg.CompanyInfo;
using MySearch;

public partial class wuc_TicketProfileForUser : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void SetPrimaryContact(Profile profile, string chargeableCode)
    {
        if (!chargeableCode.Equals("0"))
        {
            Chargeable chargeable = null;
            if (!string.IsNullOrEmpty(chargeableCode))
                chargeable = profile.Chargeables.FirstOrDefault(x => x.ChargeableCode == chargeableCode);

            IList<Staff> lst = null;
            if (chargeable != null)
                lst = chargeable.Members.Values.Select(x => x.Staff).ToList();

            SetPrimaryContact(lst);
        }


    }

    public string GetPrimaryContact()
    {
        if (this.ddlPrimaryContact.SelectedIndex != -1)
            return this.ddlPrimaryContact.SelectedItem.Value;
        else
            return "";
    }

    public void SetTicketProfile(Profile profile, string chargeableCode)
    {
        this.hfProfielCode.Value = profile.ProfileCode;
        this.hfChargeableCode.Value = chargeableCode;

        switch (profile.ProfileType)
        {
            case ProfileType.Opportunity:
                Chargeable chargeable = null;
                if (!string.IsNullOrEmpty(chargeableCode))
                    chargeable = profile.Chargeables.FirstOrDefault(x => x.ChargeableCode == chargeableCode);

                this.OppLabel.Text = profile.ProfileCode + "-" + profile.Description;
                this.ClientCodeLabel.Text = GetFieldValue(profile.Fields, "Client_Code");
                this.ClientDescLabel.Text = GetFieldValue(profile.Fields, "Client_Name");
                this.OppStatusLabel.Text = GetFieldValue(profile.Fields, "Profile_Status");
                this.OPPServiceTypeLabel.Text = "-";
                this.OppPartnerLabel.Text = GetStaffName(profile.Members, CommonMemberRole.Partner);
                this.OppManagerLabel.Text = GetStaffName(profile.Members, CommonMemberRole.Manager);
                this.InChargeLabel.Text = GetStaffName(profile.Members, CommonMemberRole.Owner);
                this.SelectedChargeableCodeLabel.Text = "";
                this.SelectedChargeableLabel.Text = "-Unassigned-";

                if (chargeable != null)
                {
                    this.SelectedChargeableCodeLabel.Text = chargeable.ChargeableCode;
                    this.SelectedChargeableLabel.Text = chargeable.Description;
                }

                break;
            case ProfileType.ServiceTeam:
                this.OppProfilePanel.Visible = false;
                this.InternalProfilePanel.Visible = true;
                this.ProfileTypeLabel.Text = "Service team ";
                this.ProfileNameLabel.Text = profile.Description;

                break;
            case ProfileType.Other:
                this.OppProfilePanel.Visible = false;
                this.InternalProfilePanel.Visible = true;
                this.ProfileTypeLabel.Text = "Internal";
                this.ProfileNameLabel.Text = "General";
                break;
        }
    }

    protected void btnSelectIO_Click(object sender, EventArgs e)
    {

    }

    //This Button Will Change The Ticket's ProfitCentreNo
    protected void btnSelectProfitCentre_Click(object sender, EventArgs e)
    {

    }

    protected void btnSwitchIO_Click(object sender, EventArgs e)
    {

    }

    protected void btnSwitchProfitCentre_Click(object sender, EventArgs e)
    {

    }

    protected void SelectOtherChargeablesButton_Click(object sender, EventArgs e)
    {
        string iid = Utilities.getLoginIID();
        Staff staff = StaffDAO.GetObjectByUserId(iid);
        if (staff == null)
            return;

        this.EmptyPanel.Visible = true;
        string profileCode = this.hfProfielCode.Value;
        IList<Chargeable> lst = new List<Chargeable>();

        IList<string> lstProfileCode = ProfileOM.ListProfileCode(staff.StaffNo);
        if (lstProfileCode != null && lstProfileCode.Contains(profileCode))
        {
            lst = ChargeableOM.ListProfileChargeable(profileCode).OrderBy(x => x.ChargeableCode).ToList();

            Chargeable obj = new Chargeable();
            obj.ChargeableCode = "0";
            obj.IsMemberInherited = true;
            obj.ChargeableType = ChargeableType.NA;

            lst.Add(obj);
        }
        else
        {
            IList<Chargeable> lstChargeable = ChargeableOM.ListMyChargeable(staff.StaffNo);
            lst = lstChargeable.Where(x => x.ProfileCode == profileCode).ToList();
        }

        if (lst.Count > 0)
        {
            this.EmptyPanel.Visible = false;
            this.rptSelectChargeablTo.Visible = true;
            this.rptSelectChargeablTo.DataSource = lst;
            this.rptSelectChargeablTo.DataBind();
        }

    }

    #region repeater

    protected void rptSelectChargeablTo_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Chargeable obj = e.Item.DataItem as Chargeable;


            Label lblType = e.Item.FindControl("lblType") as Label;
            Label lblChargeableTo = e.Item.FindControl("lblChargeableTo") as Label;
            Label lblEngDesc = e.Item.FindControl("lblEngDesc") as Label;
            Label lblEngStatus = e.Item.FindControl("lblEngStatus") as Label;
            Button btnSelectChargeable = e.Item.FindControl("btnSelectChargeable") as Button;

            if (obj.ChargeableType == ChargeableType.Engagement)
            {
                lblType.Text = obj.ChargeableType.ToString();
                lblChargeableTo.Text = obj.ChargeableCode;
                lblEngDesc.Text = obj.Description;
                lblEngStatus.Text = "Engagement status: " + obj.Status;
            }
            else
            {
                lblType.Text = "Default";
                lblChargeableTo.Text = "Chargeable TBD";
                lblChargeableTo.Font.Bold = false;
                lblChargeableTo.ForeColor = System.Drawing.Color.Black;
            }

            btnSelectChargeable.Text = "select";
        }
    }

    protected void rptSelectChargeablTo_OnItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            if (e.CommandName.Equals("SelectChargeable"))
            {
                try
                {
                    Label lblChargeableTo = e.Item.FindControl("lblChargeableTo") as Label;
                    Label lblEngDesc = e.Item.FindControl("lblEngDesc") as Label;

                    if (lblChargeableTo.Text != this.hfChargeableCode.Value)
                    {
                        if (hfChargeableCode.Value.Equals("0"))
                        {
                            this.SelectedChargeableCodeLabel.Text = "";
                            this.SelectedChargeableLabel.Text = "-Unassigned-";
                        }
                        else
                        {
                            this.SelectedChargeableCodeLabel.Text = lblChargeableTo.Text;
                            this.SelectedChargeableLabel.Text = " - " + lblEngDesc.Text;
                        }
                        this.hfChargeableCode.Value = lblChargeableTo.Text;

                        if (!hfChargeableCode.Value.Equals("0"))
                        {
                            Chargeable chargeable = ChargeableOM.GetChargeable(this.hfChargeableCode.Value, this.hfProfielCode.Value);
                            IList<Staff> lst = new List<Staff>();
                            if (chargeable != null)
                                lst = chargeable.Members.Values.Select(x => x.Staff).ToList();
                            SetPrimaryContact(lst);
                        }

                    }
                    this.rptSelectChargeablTo.Visible = false;
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
    }

    #endregion

    #region private

    private string GetFieldValue(IDictionary<string, CommonField> dicFields, string fieldName)
    {
        if (dicFields == null || dicFields.Count() < 1)
            return "";

        CommonField cf = dicFields.Values.FirstOrDefault(x => x.Name == fieldName);
        if (cf != null)
            return cf.Value;

        return "";
    }

    private string GetStaffName(IDictionary<string, CommonMember> dicMember, CommonMemberRole role)
    {
        if (dicMember == null || dicMember.Count() < 1)
            return "";

        CommonMember cm = dicMember.Values.FirstOrDefault(x => x.Role == role);
        if (cm != null && cm.Staff != null)
            return cm.Staff.DisplayName;

        return "";
    }

    private void SetPrimaryContact(IList<Staff> lst)
    {
        lst = lst.OrderBy(x => x.DisplayName).Distinct().ToList();
        Dictionary<string, string> dic = new Dictionary<string, string>();
        dic = lst.ToDictionary(x => x.StaffNo, y => y.DisplayName);
        dic.Add("", " Please select Primary contact");
        dic = dic.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);


        this.ddlPrimaryContact.DataSource = dic;
        this.ddlPrimaryContact.DataTextField = "Value";
        this.ddlPrimaryContact.DataValueField = "Key";
        this.ddlPrimaryContact.DataBind();

    }

    #endregion

}